package com.example.hp.iclass.TeacherCheckActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;

import com.example.hp.iclass.CommonActivity.MainActivity;
import com.example.hp.iclass.HttpFunction.Function.Student_Fuction.Fun_UpdateStudentDeviceCode;
import com.example.hp.iclass.HttpFunction.Function.Teacher_Function.Fun_UpdateTeacherDeviceCode;
import com.example.hp.iclass.OBJ.TeacherOBJ;
import com.example.hp.iclass.R;

public class UpdateDeviceCodeActivity extends AppCompatActivity implements View.OnClickListener {
    private TeacherOBJ teacherOBJ = new TeacherOBJ();
    private EditText edit_id;
    private Button bt_con;
    private RadioButton choice_student;
    private RadioButton choice_teacher;
    private Toolbar tl_head;
    private int user_choice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_device_code);
        tl_head = (Toolbar) findViewById(R.id.tl_head);
        tl_head.setNavigationIcon(R.drawable.ic_back);
        tl_head.setTitle("为其他用户解除设备绑定");
        tl_head.setTitleTextColor(Color.WHITE);
        tl_head.setNavigationIcon(R.drawable.ic_back);
        setSupportActionBar(tl_head);
        tl_head.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gotomain();
            }
        });
        edit_id = (EditText) findViewById(R.id.edit_id);
        bt_con = (Button) findViewById(R.id.bt_con);
        bt_con.setOnClickListener(this);
        choice_student = (RadioButton) findViewById(R.id.user_student);
        choice_teacher = (RadioButton) findViewById(R.id.user_teacher);
        choice_student.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (choice_student.isChecked()) {
                    user_choice = 0;
                }
            }
        });
        choice_teacher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (choice_teacher.isChecked()) {
                    user_choice = 1;
                }
            }
        });
    }

    private void gotomain() {
        Intent it = new Intent(UpdateDeviceCodeActivity.this, MainActivity.class);
        it.putExtra("teacherOBJ", teacherOBJ);
        it.putExtra("user", "teacher");
        it.putExtra("to_check", "true");
        startActivity(it);
        finish();
    }

    public void onBackPressed() {
        gotomain();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_con) {
            dialog();
        }
    }

    private void dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(UpdateDeviceCodeActivity.this);
        builder.setMessage("确认解除该用户与设备的绑定吗？");
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle("警告");
        builder.setCancelable(true);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (user_choice == 0) { //student
                            String student_id = edit_id.getText().toString().trim();
                            try {
                                if (Fun_UpdateStudentDeviceCode.http_UpdateStudentDeviceCode(student_id, student_id)) {
                                    dialog_success();
                                } else {
                                    dialog_failed();
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else if (user_choice == 1) {   //teacher
                            String teacher_id = edit_id.getText().toString().trim();
                            try {
                                if (Fun_UpdateTeacherDeviceCode.http_UpdateTeacherDeviceCode(teacher_id, teacher_id)) {
                                    dialog_success();
                                } else {
                                    dialog_failed();
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        dialog.dismiss();//关闭对话框
                    }
                }
        );
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();    //显示对话框
    }

    void dialog_success() {
        AlertDialog.Builder builder = new AlertDialog.Builder(UpdateDeviceCodeActivity.this);
        builder.setMessage("解绑成功！");
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle("警告");
        builder.setCancelable(false);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();//关闭对话框
                    }
                }
        );
        builder.create().show();////显示对话框
    }

    void dialog_failed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(UpdateDeviceCodeActivity.this);
        builder.setMessage("解绑失败！");
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle("警告");
        builder.setCancelable(false);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();//关闭对话框
                    }
                }
        );
        builder.create().show();////显示对话框
    }
}
